package com.woniuxy.controller;

import com.woniuxy.annotation.PassLevelAnnotation;
import com.woniuxy.entity.dto.operations.*;
import com.woniuxy.service.ClientService;
import com.woniuxy.util.CodeUtil;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @BelongsProject: charging-management-platform
 * @BelongsPackage: com.woniuxy.controller
 * @Author: Quan
 * @Date: 2025/3/30 15:40
 */
@RestController
@RequestMapping("/client")
@AllArgsConstructor
public class ClientController {
	private ClientService clientService;
	private CodeUtil codeUtil;

	/**
	 * @Description: 用于选择时展示客户
	 * @Param: []
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/3/30
	 */
	@PostMapping("/queryClientToDisplay")
	public ResponseData<?> queryClientToDisplay(@RequestBody PageParam<ClientForCardSelectDisplayDTO> pageParam) {
		return clientService.queryAllClientToSelect(pageParam);
	}

	/**
	 * @Description: 查询全部数据并展示
	 * @Param: [pageParam]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/3/31
	 */
	@PostMapping("/queryAll")
	public ResponseData<?> queryAll(@RequestBody PageParam<ClientDisplayDTO> pageParam) {
		return clientService.queryAll(pageParam);
	}

	/**
	 * @Description: 根据id对用户进行禁用
	 * @Param: [id]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/3/31
	 */
	@GetMapping("/forbiddenById/{id}")
	public ResponseData<?> forbiddenById(@PathVariable("id") Long id) {
		return clientService.forbiddenById(id);
	}

	/**
	 * @Description: 通过id进行删除
	 * @Param: [id]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/3/31
	 */
	@GetMapping("/delete/{id}")
	public ResponseData<?> delete(@PathVariable("id") Long id) {
		return clientService.delete(id);
	}

	/**
	 * @Description: 当新增用户时，先从后端获取编码
	 * @Param: []
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/2
	 */
	@GetMapping("/getCode")
	public ResponseData<?> getCode() {
		return ResponseData.ok(codeUtil.generateRegularCode("CLIENT"));
	}

	/**
	 * @Description: 获取用户的类型
	 * @Param: []
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/2
	 */
	@GetMapping("/getClientType")
	public ResponseData<?> getClientType() {
		return clientService.queryClientType();
	}

	/**
	 * @Description: 通过id查询对应的client详细信息
	 * @Param: [id]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/2
	 */
	@GetMapping("/queryClientDetailById/{id}")
	public ResponseData<?> queryClientDetailById(@PathVariable("id") Long id) {
		return clientService.queryClientBindVehicleByClientId(id);
	}

	/**
	 * @Description: 上传图片，并返回地址
	 * @Param: [file]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/2
	 */
	@PostMapping("/upload")
	public ResponseData<?> uploadFile(MultipartFile file) {
		try {
			return clientService.uploadImg(file);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * @Description: 通过车辆id在车辆中的用户进行解绑
	 * @Param: [id]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/2
	 */
	@PostMapping("/unbindVehicle")
	public ResponseData<?> unbindVehicle(@RequestBody UnbindVehicleDTO unbindVehicleDTO) {
		return clientService.unbindVehicle(unbindVehicleDTO);
	}

	/**
	 * @Description: 在用户的更新页面中， 当点击添加车辆，展示给用户没有绑定客户的车辆
	 * @Param: [pageParam]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/2
	 */
	@PostMapping("/queryUnbindVehicle")
	public ResponseData<?> queryUnbindVehicle(@RequestBody PageParam<VehicleDisplayForClientDetailDTO> pageParam) {
		return clientService.queryUnbindVehicle(pageParam);
	}

	/**
	 * @Description: 通过车辆id在车辆中的用户进行绑定
	 * @Param: [id]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/2
	 */
	@PostMapping("/bindVehicle")
	public ResponseData<?> bindVehicle(@RequestBody UnbindVehicleDTO unbindVehicleDTO) {
		return clientService.bindVehicle(unbindVehicleDTO);
	}

	/**
	 * @Description: 更新客户信息到数据库中
	 * @Param: [clientDetailDTO]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/3
	 */
	@PostMapping("/update")
	public ResponseData<?> update(@RequestBody ClientDetailDTO clientDetailDTO) {
		return clientService.update(clientDetailDTO);
	}

	/**
	 * @Description: 获取支付类型
	 * @Param: []
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/3
	 */
	@GetMapping("/getAccountType")
	public ResponseData<?> queryAccountType() {
		return clientService.queryAccountType();
	}

	/**
	 * @Description: 将客户添加到数据库中
	 * @Param: []
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/3
	 */
	@PassLevelAnnotation()
	@PostMapping("/addClient")
	public ResponseData<?> addClient(@RequestBody ClientDetailDTO clientDetailDTO) {
		ResponseData<?> responseData = clientService.addClient(clientDetailDTO);
		return responseData;
	}

	/**
	 * @Description: 通过用户id批量进行删除
	 * @Param: [ids]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/7
	 */
	@PostMapping("/batchDelete")
	public ResponseData<?> batchDeleteClient(@RequestBody List<Long> ids) {
		return clientService.batchDelClient(ids);
	}

	/**
	 * @Description: 主要是更新用户的持有车数据
	 * @Param: []
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/7
	 */
	@PostMapping("/refresh")
	public ResponseData<?> refresh() {
		return clientService.refresh();
	}
	/** 
	 * @Description: 查询client表的数量，得到注册总人数
	 * @Param: []
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: DirtyFlower
	 * @Date: 2025/4/7
	 */
	@GetMapping("/queryClientNumber")
	public ResponseData<?> queryClientNumber(){
		return clientService.queryClientNumber();
	}
	/**
	 * @Description: 根据日期查询client表的数量，得到今天注册总人数
	 * @Param: []
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: DirtyFlower
	 * @Date: 2025/4/7
	 */
	@GetMapping("/queryClientNumberToday")
	public ResponseData<?> queryClientNumberToday(){
		return clientService.queryClientNumberToday();
	}
}
